package com.demo.demo.single_property;

import com.demo.demo.TestConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ayatsuji
 * @date 2021-06-13 22:25
 * @since
 */
@Component
public class SinglePropertyDemo implements TestConfig.Show {
    @Value("${single.name}")
    private String name;

    @Value("${single.age}")
    private Integer age;

    @Value("${single.hobby}")
    private List<String> hobbyList;
    @Value("${single.hobby}")
    private Set<String> hobbySet;
    @Value("${single.hobby}")
    private String[] hobbyArray;

    @Autowired
    private Hobby2 hobby2;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void showAll() {
        showName();
        showAge();
        showHobby();
        showHobby2();
    }

    private void showName() {
        logger.info("name=" + name);
    }

    private void showAge() {
        logger.info("age=" + age);
    }

    private void showHobby() {
        logger.info("hobbyArray=" + Arrays.toString(hobbyArray));
        logger.info("hobbyList=" + hobbyList);
        logger.info("hobbySet=" + hobbySet);
    }

    private void showHobby2() {
        logger.info("hobby2=" + hobby2.getHobby2());
    }

    // 使用@ConfigurationProperties要给个setter方法
    @Component
    @ConfigurationProperties("single")
    static class Hobby2 {
        private List<String> hobby2;

        public void setHobby2(List<String> hobby2) {
            this.hobby2 = hobby2;
        }

        public List<String> getHobby2() {
            return hobby2;
        }
    }
}
